import {Component, OnInit, Injector, TemplateRef, ViewChild, Input } from '@angular/core';
import {NgZorroPageBaseComponent} from '../../core/ng-zorro-page-base-component/ng-zorro-page-base-component.component';
import {HttpService} from "../../core/service/http.service";
import {Router, NavigationExtras} from "@angular/router";
import {
  AndOr, Compare, ExpressionPair, PagedResultDto, PagerSearchInput, ElementViewType, BaseComponentConfig,
  SystemFormControl, FieldType, ElementShowType, CustomNavigation, NavigatType
} from "../..";
import {NzMessageService, NzModalService} from 'ng-zorro-antd';
import {
  FormBuilder, FormGroup, FormsModule, ReactiveFormsModule, Validators,
  FormControl
} from "@angular/forms";
import {SystemFormOutput} from "../../core/model/SystemFormDto";
import {SystemFormControlComponent} from "./system-form-control.component";


@Component({
  selector: 'app-system-form',
  templateUrl: '../../core/ng-zorro-page-base-component/ng-zorro-page-base-component.component.html',
  styleUrls: ['../../core/ng-zorro-page-base-component/ng-zorro-page-base-component.component.scss']
})
export class SystemFormComponent extends NgZorroPageBaseComponent implements OnInit {

  constructor(injector: Injector) {
    super(injector,
      new BaseComponentConfig(
        "SystemForm/GetAll",
        "SystemForm/Create",
        "SystemForm/Update",
        "SystemForm/Get",
        "SystemForm/Delete",
        "id",
        "SystemForm",
        "表单信息管理",
        new Array<CustomNavigation>(
          new CustomNavigation('字段管理', '', ElementShowType.TableOperation, 'anticon anticon-bars', NavigatType.Model)
        )
      ));
  }

  getNewEntity(): SystemFormOutput {
    return new SystemFormOutput(0, "", "");
  }

  async navigateCustom(url: string, pid: any) {
    const msgId = this.message.loading('正在获取数据', {nzDuration: 0}).messageId;
    try {
      const findEntity = await this.httpService.get<SystemFormOutput>(`${this.componentConfig.GetSingle}?input=${pid}`);
      if (findEntity) {
        const navigationExtras: NavigationExtras = {
          queryParams: {'id': pid, 'caption': findEntity.formCaption}
        };
        this.router.navigate([url], navigationExtras);
      } else {
        this.message.error('无法确定表单信息！请刷新列表！');
      }
    } finally {
      this.message.remove(msgId);
    }

  }

  showModalForComponent(entity: SystemFormOutput) {
    const keyValue = entity.id;
    const subscription = this.nzModalService.create({
      nzTitle: `${entity.formName}-${entity.formCaption}`,
      nzContent: SystemFormControlComponent,
      nzFooter: null,
      nzComponentParams: {
        pKeyFieldValue: keyValue,
        caption: entity.formName
      },
      nzWidth: document.documentElement.clientWidth - 60,
    });
  }
}
